<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/22 0022
 * Time: 11:06
 */
namespace App\Http\Controllers;
use App\Http\Requests\StoreRequest;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class ContentConroller extends  BaseController{
    public function  index(StoreRequest $request){
        $request = $request->all();
        $map = array();
        isset($request['title']) ? $map['title'] = $request['title'] : '';
        isset($request['name']) ? $map['name'] = $request['name'] : '';

        $lists = DB::table('content')->where($map)->leftJoin('tour_menu', 'tour_menu.menu_id', '=', 'content.menu_id')->paginate(10);
        return view('content.index',['lists'=>$lists]);
    }

    //编辑或者增加
    public function edit($id=0){
        $info = Db::table('content')->where('id',$id)->first();
        $menu = Db::table('tour_menu')->where('parent_id','0')->get();
        $menu = $this->objectToArray($menu);
        foreach($menu as $k=>&$v){
            $menu_id =  $v['menu_id'];
            $chield = Db::table('tour_menu')->where('parent_id',$menu_id)->get();
            $chielddata = $this->objectToArray($chield);
            $v['child'] = $chielddata;
        }

        return view('content.edit',['id'=>$id,'info'=>$info,'menu'=>$menu]);
    }


    public  function  store(StoreRequest $request){
        $id = Auth::id();  //登录用户id
        $map['user_id'] = $id;
        $arr = $request->all();
        $map['title'] = $arr['title'];
        $map['img'] = $arr['img'];
        $map['content'] = $arr['content'];
        $map['price'] = $arr['price'];
        $map['commission'] = $arr['commission'];
        !empty($arr['newest']) ? $map['newest'] = $arr['newest']: '';
        !empty($arr['highcommission']) ?  $map['highcommission'] = $arr['highcommission']: '';
        !empty($arr['sellwell']) ?   $map['sellwell'] = $arr['sellwell'] : '';

        !empty($arr['agency_id'])? $map['agency_id'] = $arr['agency_id']: '';

        $map['menu_id'] = $arr['menu_id'];
        $map['setime'] = $arr['setime'];
        // $map['user_id'] = Session::
        if($arr['id'] == '0'){
            Db::table('content')->insert($map);
        }else{
            Db::table('content')->where('id',$arr['id'])->update($map);
        }
        return ['status'=>1,'msg'=>trans('添加成功')];
    }

    /**
     * @param $id
     * 删除菜单
     */
    public function destroy($id){
        $menu = DB::table('content')->where('id',$id)->delete();
        return ['status'=>1,'msg'=>trans('删除成功')];
    }
    /**
     * 上传图片
     *
     */
    public function upload(StoreRequest $request){
        $file = $request->file('img');
        // 此时 $this->upload如果成功就返回文件名不成功返回false
        $fileName = $this->uploadfile($file);
        if ($fileName){
            return json_encode(['code'=>'200','imgurl'=>$fileName,'msg'=>'上传成功']);
           // return $fileName;
        }
        return json_encode(['code'=>'101','msg'=>'上传失败']);
    }
    public function uploadfile($file, $disk='public') {
        // 1.是否上传成功


        // 2.是否符合文件类型 getClientOriginalExtension 获得文件后缀名
        $fileExtension = $file->getClientOriginalExtension();
        if(! in_array($fileExtension, ['png', 'jpg', 'gif'])) {
            return false;
        }

        // 3.判断大小是否符合 2M
        $tmpFile = $file->getRealPath();
        if (filesize($tmpFile) >= 2048000) {
            return false;
        }

        // 4.是否是通过http请求表单提交的文件
        if (! is_uploaded_file($tmpFile)) {
            return false;
        }

        // 5.每天一个文件夹,分开存储, 生成一个随机文件名
        $fileName =date('Ymd').'/'.md5(time()) .mt_rand(0,9999).'.'. $fileExtension;
        if (Storage::disk('uploads')->put($fileName, file_get_contents($tmpFile)) ){
            return($fileName);
           //return Storage::url($fileName);
        }
    }
    function objectToArray($object) {
        //先编码成json字符串，再解码成数组
        return json_decode(json_encode($object), true);
    }


}